<script setup lang="ts">
import { onMounted, onUnmounted, ref, shallowRef } from 'vue'
import {
  AxesHelper,
  Scene,
  PerspectiveCamera,
  SphereGeometry,
  WebGLRenderer,
  BoxGeometry,
  MeshStandardMaterial,
  Mesh,
  Plane,
  PlaneGeometry,
  AmbientLight,
  DirectionalLight,
  Vector3,
} from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { GUI } from 'dat.gui'
import Stats from 'three/examples/jsm/libs/stats.module'
import Cannon from 'cannon-es'

const contentRef = ref<HTMLCanvasElement>()
const renderer = shallowRef<WebGLRenderer>(new WebGLRenderer())
const orbitControls = shallowRef<OrbitControls>()
const scene = new Scene()
const camera = shallowRef<PerspectiveCamera>()
const axesHelper = new AxesHelper(20)
// 创建性能监视器
let stats = new Stats()

const gui = new GUI()

const init = () => {
  if (contentRef.value) {
    camera.value = new PerspectiveCamera(75, contentRef.value.clientWidth / contentRef.value.clientHeight)
    camera.value.position.set(0, 10, 20)
    scene.add(camera.value)
    scene.add(axesHelper)
    contentRef.value.appendChild(renderer.value.domElement)

    stats.dom.style.position = 'absolute'
    stats.dom.style.top = '0'
    stats.dom.style.left = '0'
    contentRef.value.appendChild(stats.dom)

    renderer.value.setSize(contentRef.value?.clientWidth, contentRef.value?.clientHeight)

    orbitControls.value = new OrbitControls(camera.value, renderer.value.domElement)
    orbitControls.value.enableDamping = true
  }
}

const render = () => {
  if (camera.value) {
    orbitControls.value?.update()
    renderer.value?.render(scene, camera.value)
  }
  stats.update()
  requestAnimationFrame(render)
}

window.addEventListener('resize', () => {
  if (contentRef.value && camera.value) {
    camera.value.aspect = contentRef.value.clientWidth / contentRef.value.clientHeight
    camera.value.updateProjectionMatrix()

    renderer.value.setSize(contentRef.value?.clientWidth, contentRef.value?.clientHeight)
    renderer.value.setPixelRatio(window.devicePixelRatio)
  }
})

onMounted(() => {
  init()
  const sphereGeometry = new SphereGeometry(1, 20, 20)
  const sphereMaterial = new MeshStandardMaterial({ color: '#fff' })
  const sphereMesh = new Mesh(sphereGeometry, sphereMaterial)

  const planeMesh = new Mesh(new PlaneGeometry(20, 20), new MeshStandardMaterial({ color: '#fff' }))
  planeMesh.position.y = -5
  planeMesh.rotateX(-Math.PI / 2)

  const ambientLight = new AmbientLight('#fff', 0.5)
  const directionalLight = new DirectionalLight('#fff', 0.4)

  // 开启阴影
  renderer.value.shadowMap.enabled = true
  sphereMesh.castShadow = true
  planeMesh.receiveShadow = true
  directionalLight.castShadow = true

  scene.add(sphereMesh)
  scene.add(planeMesh)
  scene.add(ambientLight)
  scene.add(directionalLight)

  const physicWorld = new Cannon.World({ gravity: new Cannon.Vec3(0, -9.82, 0) })
  const sphere = new Cannon.Sphere(1)
  const spherePhysicMaterial = new Cannon.Material()

  render()
})

onUnmounted(() => {
  gui.destroy()
})
</script>

<template>
  <div class="physics" ref="contentRef"></div>
</template>

<style lang="scss" scoped>
.physics {
  width: 100%;
  height: 100%;
  position: relative;
}
</style>
